package com.core136.service.hr;

import com.core136.bean.account.Account;
import com.core136.bean.hr.HrIncentive;
import com.core136.bean.sys.PageParam;
import com.core136.common.enums.MessageCode;
import com.core136.common.retdataunit.RetDataBean;
import com.core136.common.retdataunit.RetDataTools;
import com.core136.common.utils.ExcelUtil;
import com.core136.common.utils.SysTools;
import com.core136.mapper.hr.HrIncentiveMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HrIncentiveService {
    private HrIncentiveMapper hrIncentiveMapper;

    @Autowired
    public void setHrIncentiveMapper(HrIncentiveMapper hrIncentiveMapper) {
        this.hrIncentiveMapper = hrIncentiveMapper;
    }

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int insertHrIncentive(HrIncentive hrIncentive) {
        return hrIncentiveMapper.insert(hrIncentive);
    }

    public int deleteHrIncentive(HrIncentive hrIncentive) {
        return hrIncentiveMapper.delete(hrIncentive);
    }

    public int updateHrIncentive(Example example, HrIncentive hrIncentive) {
        return hrIncentiveMapper.updateByExampleSelective(hrIncentive, example);
    }

    public HrIncentive selectOneHrIncentive(HrIncentive hrIncentive) {
        return hrIncentiveMapper.selectOne(hrIncentive);
    }

    /**
     * @param orgId
     * @param userId
     * @param beginTime
     * @param endTime
     * @param incentiveType
     * @param incentiveItem
     * @return List<Map < String, String>>
     * @Title: getHrIncentiveList
     * @Description:  获取奖惩记录列表
     */
    public List<Map<String, String>> getHrIncentiveList(String orgId, String userId, String beginTime, String endTime, String incentiveType, String incentiveItem) {
        return hrIncentiveMapper.getHrIncentiveList(orgId, userId, beginTime, endTime, incentiveType, incentiveItem);
    }

    /**
     * @param orgId
     * @param accountId
     * @return List<Map < String, String>>
     * @Title: getMyHrIncentiveList
     * @Description:   个人查询奖惩记录
     */
    public List<Map<String, String>> getMyHrIncentiveList(String orgId, String accountId) {
        return hrIncentiveMapper.getMyHrIncentiveList(orgId, accountId);
    }

    /**
     * @param pageParam
     * @param userId
     * @param beginTime
     * @param endTime
     * @param incentiveType
     * @param incentiveItem
     * @return PageInfo<Map < String, String>>
     * @throws Exception
     * @Title: getHrIncentiveList
     * @Description:  获取奖惩记录列表
     */
    public PageInfo<Map<String, String>> getHrIncentiveList(PageParam pageParam, String userId, String beginTime, String endTime, String incentiveType, String incentiveItem) throws Exception {
        PageHelper.startPage(pageParam.getPageNumber(), pageParam.getPageSize(), SysTools.escapeSqlStr(pageParam.getOrderBy()));
        List<Map<String, String>> datalist = getHrIncentiveList(pageParam.getOrgId(), userId, beginTime, endTime, incentiveType, incentiveItem);
        PageInfo<Map<String, String>> pageInfo = new PageInfo<Map<String, String>>(datalist);
        return pageInfo;
    }

    /**
     * @param pageParam
     * @return PageInfo<Map < String, String>>
     * @throws Exception
     * @Title: getMyHrIncentiveList
     * @Description: 个人查询奖惩记录
     */
    public PageInfo<Map<String, String>> getMyHrIncentiveList(PageParam pageParam) throws Exception {
        PageHelper.startPage(pageParam.getPageNumber(), pageParam.getPageSize(), SysTools.escapeSqlStr(pageParam.getOrderBy()));
        List<Map<String, String>> datalist = getMyHrIncentiveList(pageParam.getOrgId(), pageParam.getAccountId());
        PageInfo<Map<String, String>> pageInfo = new PageInfo<Map<String, String>>(datalist);
        return pageInfo;
    }


    /**
     * @param account
     * @param file
     * @return IOException
     * RetDataBean
     * @Title: importHrIncentive
     * @Description:  奖惩记录导入
     */
    @Transactional(value = "generalTM")
    public RetDataBean importHrIncentive(Account account, MultipartFile file) throws IOException {
        Map<String, String> fieldMap = new HashMap<String, String>();
        fieldMap.put("排序号", "sort_no");
        fieldMap.put("奖惩类型", "incentive_type");
        fieldMap.put("涉及人员", "user_id");
        fieldMap.put("处理事项", "incentive_item");
        fieldMap.put("处理日期", "incentive_time");
        fieldMap.put("奖惩金额", "incentive_amount");
        fieldMap.put("工资月份", "salary_month");
        fieldMap.put("备注", "remark");
        List<String> fieldList = new ArrayList<String>();
        List<String> titleList = new ArrayList<String>();
        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
            fieldList.add(entry.getValue());
            titleList.add(entry.getKey());
        }
        String[] fieldArr = new String[fieldList.size()];
        fieldList.toArray(fieldArr);
        String fieldString = StringUtils.join(fieldArr, ",");
        List<Map<String, String>> recordList = ExcelUtil.readExcel(file);
        for (int i = 0; i < recordList.size(); i++) {
            Map<String, String> tempMap = recordList.get(i);
            String valueString = "'" + SysTools.getGUID() + "',";
            for (int k = 0; k < titleList.size(); k++) {
                if (titleList.get(k).equals("涉及人员")) {
                    if (StringUtils.isNotBlank(tempMap.get(titleList.get(k)))) {
                        String sql1 = "select user_id from hr_user_info where user_name ='" + tempMap.get(titleList.get(k)) + "' and org_id='" + account.getOrgId() + "'";
                        String userId = jdbcTemplate.queryForObject(sql1, String.class);
                        valueString += "'" + userId + "',";
                    } else {
                        valueString += "'',";
                    }
                } else {
                    valueString += "'" + tempMap.get(titleList.get(k)) + "',";
                }
            }
            valueString += "'" + SysTools.getTime("yyyy-MM-dd HH:mm:ss") + "',";
            valueString += "'" + account.getAccountId() + "',";
            valueString += "'" + account.getOrgId() + "'";
            String insertSql = "insert into hr_incentive(incentive_id," + fieldString + ",create_time,create_user,org_id) values" + "(" + valueString + ")";
            jdbcTemplate.execute(insertSql);
        }
        return RetDataTools.Ok(MessageCode.MESSAGE_SUCCESS);
    }

}
